//
//  RemindViewController.m
//  Eath
//
//  Created by mao on 11/2/14.
//  Copyright (c) 2014 yuhaya. All rights reserved.
//

#import "RemindViewController.h"
#import "CustomActionSheet.h"
#import "CustomAlertController.h"
#import "Tool.h"

@interface RemindViewController ()

@end

@implementation RemindViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"提醒设置"];
    customLab.textAlignment = NSTextAlignmentCenter;
    customLab.font = [UIFont boldSystemFontOfSize:16];
    self.navigationItem.titleView = customLab;
    
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, [Tool tableHeight]) style:UITableViewStyleGrouped];
    self.table.delegate = self;
    self.table.dataSource = self;
    [self.view addSubview:self.table];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0 || section == 4){
        return 1;
    }else if(section == 1 || section == 2 || section == 3 || section ==5){
        return 2;
    }else{
        return 1;
    }
}


// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    
    if(indexPath.section == 0 && indexPath.row == 0){
        
        UILabel * text = [[UILabel alloc] init];
        text.text = @"到离校时间点设置";
        text.frame = CGRectMake(20, 0, 320, 40);
        [cell.contentView addSubview:text];
        
    }else if(indexPath.section == 1 || indexPath.section == 2 || indexPath.section == 3){
        
        UILabel * text = [[UILabel alloc] init];
        text.frame = CGRectMake(30, 0, 100, 40);
        [cell.contentView addSubview:text];
        
        switch (indexPath.row) {
            case 0:
                text.text = @"到校时间";
                break;
            case 1:
                text.text = @"离校时间";
                break;
            default:
                break;
        }
        
        UILabel * time = [[UILabel alloc] initWithFrame:CGRectMake(150, 0, 150, 40)];
        time.textAlignment = NSTextAlignmentRight;
        time.tag = 1;
        time.text = [self timeSettingAtIndexPath:indexPath] ? [self timeSettingAtIndexPath:indexPath] : @"00:00";
        [cell.contentView addSubview:time];
        
    }else if(indexPath.section == 4){
        
        NSString *labelText;
            
        labelText = @"超过时间点10分钟未 \n收到消息则给出预警";
        
        NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:labelText];
        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
        [paragraphStyle setLineSpacing:5];
        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [labelText length])];
        
        UILabel * text = [[UILabel alloc] init];
        text.numberOfLines = 0;
        text.attributedText = attributedString;
        text.frame = CGRectMake(20, 0, 200, 80);
        [cell.contentView addSubview:text];
        
        UISwitch * sign = [[UISwitch alloc] init];
        [sign addTarget:self action:@selector(switch:) forControlEvents:UIControlEventValueChanged];
        sign.tag = indexPath.section + indexPath.row;
        [sign setOn:[self getSwitchValByItem:[self getItem:sign]] animated:YES];
        sign.frame = CGRectMake(250, 25, 100, 60);
        [cell.contentView addSubview:sign];
        
    }else if(indexPath.section == 5){
        
        UILabel * text = [[UILabel alloc] init];
        text.frame = CGRectMake(30, 0, 100, 40);
        [cell.contentView addSubview:text];
        
        switch (indexPath.row) {
            case 0:
                text.text = @"响铃";
                break;
            case 1:
                text.text = @"震动";
            default:
                break;
        }
        
        UISwitch * sign = [[UISwitch alloc] init];
        [sign addTarget:self action:@selector(switch:) forControlEvents:UIControlEventValueChanged];
        sign.tag = indexPath.section + indexPath.row;
        [sign setOn:[self getSwitchValByItem:[self getItem:sign]] animated:YES];
        sign.frame = CGRectMake(250, 5, 100, 40);
        [cell.contentView addSubview:sign];
    }
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)switch:(UISwitch *)sender
{
    NSString * item = [self getItem:sender];
    
    [Tool setUserSetting:self.stu.identify andItem:item andVal:[NSString stringWithFormat:@"%d",sender.on ? 1 : 0]];
}

- (NSString *)getItem:(UISwitch *)sender
{
    NSString * item;
    
    if(sender.tag == 5){
        
        item = @"SOUND";
    }else if (sender.tag == 6){
        
        item = @"SHAKE";
    }else if (sender.tag == 4){
        
        item = @"DELAY_NOTICE";
    }
    return item;
}

- (BOOL)getSwitchValByItem:(NSString *)item
{
    NSString * select = [Tool getUserSetting:self.stu.identify andItem:item];
    if([select intValue] == 1){
        return YES;
    }else{
        return NO;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 6;
}

- (NSString *)timeSettingAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * item = (NSString *)[self itemAtIndexPath:indexPath];
    return [Tool getUserSetting:self.stu.identify andItem:item];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if(section == 1){
        
        return @"上午";
    }else if(section == 2){
        
        return @"下午";
    }else if (section == 3){
        
        return @"晚间";
    }else if(section == 5){
        
        return @"提醒方式";
    }else{
        
        return @"";
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    return @"";
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    float version = [[[UIDevice currentDevice] systemVersion] floatValue];
    
    if(version >= 8.0){
    
        CustomAlertController * ca = [CustomAlertController alertControllerWithTitle:@"" message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
        ca.config_item = [self itemAtIndexPath:indexPath];
        ca.stu = self.stu;
        UITableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
        UILabel * label = (UILabel *)[cell viewWithTag:1];
        ca.label = label;
        [self presentViewController:ca animated:YES completion:nil];
        
    }else{
        
        CustomActionSheet* sheet = [[CustomActionSheet alloc] initWithHeight:284.0f WithSheetTitle:@"选择日期"];
//        sheet.stu = self.stu;
        [sheet showInView:self.view];
    }
}

- (NSString *) itemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 1){
        if (indexPath.row == 0) {
            return @"AM_I";
        }else if (indexPath.row == 1){
            return @"AM_O";
        }
    }else if (indexPath.section == 2){
        if (indexPath.row == 0) {
            return @"PM_I";
        }else if (indexPath.row == 1){
            return @"PM_O";
        }
    }else if (indexPath.section == 3){
        if (indexPath.row == 0) {
            return @"NM_I";
        }else if (indexPath.row == 1){
            return @"NM_O";
        }
    }else if (indexPath.section == 5){
        if (indexPath.row == 0) {
            return @"SOUND";
        }else if (indexPath.row == 1){
            return @"SHAKE";
        }
    }else if (indexPath.section == 4){
    
        return @"DELAY_NOTICE";
    }
    
    return @"";
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 0){
        return 40;
    }else if (indexPath.section == 1 || indexPath.section == 2 || indexPath.section == 3 || indexPath.section == 5){
        return 40;
    }else{
        return 80;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
